package com.nsqt.biz.controller;

import com.nsqt.biz.domain.SoftwareDO;
import com.nsqt.biz.service.AgentService;
import com.nsqt.biz.service.SoftwareService;
import com.nsqt.common.utils.*;
import com.nsqt.system.domain.UserDO;
import com.nsqt.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 软件
 */
@Controller
@RequestMapping("/biz/submit")
public class SubmitSoftwareController {
    @Autowired
    private SoftwareService softwareService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private UserService userService;

    @GetMapping()
    @RequiresPermissions("biz:submit:submit")
    String Submit() {
        return "biz/submit/submit";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("biz:submit:submit")
    public PageUtils submitlist(@RequestParam Map<String, Object> params, Model model) {
        Long userId = ShiroUtils.getUserId();
        UserDO user = userService.get(userId);
        List<Long> roles = user.getRoleIds();
        params.put("status", 9);
        //查询列表数据
        Query query = new Query(params);

        query.put("sort", "submit_date");
        query.put("order", "desc");
        List<SoftwareDO> softwareList = softwareService.listUnsubmit(query);
        for (SoftwareDO software : softwareList) {
            software.setSubmitDate(DateUtils.date2Str(new Date(), "YYYY-MM-dd"));

        }

        int total = softwareService.countPresubmit(query);
        PageUtils pageUtils = new PageUtils(softwareList, total);

        return pageUtils;
    }

    /**
     * 预提交
     */
    @PostMapping("/presubmit")
    @ResponseBody
    @RequiresPermissions("biz:submit:submit")
    public R preSubmit(String sn) {
        SoftwareDO software = softwareService.getBySn(sn.trim());
        if (software == null) {
            return R.error(0, "没有这个流水号！");
        }
        software.setSubmitDate(DateUtils.date2Str(new Date(), "YYYY-MM-dd hh:mm:ss"));
        //software.setBusinessMan(ShiroUtils.getUserId().intValue());
        if (software.getBusinessType() == null) {
            software.setBusinessType(0);
        }

        if (software.getWorkDays() == null) {
            software.setWorkDays(31);
            software.setPrice("300");
            String approvalDate = DateUtils.date2Str(DateUtils.getDate(new Date(), 31), "YY-MM-dd");
            software.setApprovalDate(approvalDate);
        } else {
            int workDay = software.getWorkDays();
            software.setPrice(String.valueOf(software.getPrice(workDay)));
            String approvalDate = DateUtils.date2Str(DateUtils.getDate(new Date(), workDay), "YYYY-MM-dd");
            software.setApprovalDate(approvalDate);

        }
        //System.out.println(software.toString());
        software.setStatus(9);

        if (softwareService.update(software) > 0) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 取消提交
     */
    @PostMapping("/cancel")
    @ResponseBody
    @RequiresPermissions("biz:submit:cancel")
    public R cancelSubmit(Integer id) {
        if (softwareService.cancelSubmit(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除软件
     */
    @PostMapping("/batchSubmit")
    @ResponseBody
    @RequiresPermissions("biz:submit:submit")
    public R batchSubmit(@RequestParam("ids[]") Integer[] ids) {

        softwareService.batchSubmit(ids);
        return R.ok();
    }

}
